setwd("/home1/30/jc227089/SRE/gvar_adap/ran_outs")

ncomb<-length(list.files())
#first concatenation
# to generate, for each parameter combo, mean across reps of mean (across time) of trait values through space
concat<-c()
for (i in 1:ncomb){
	fname<-paste("gvarout", i, ".RData", sep="")
	if (file.exists(fname)==F) next
	load(fname)
	temp<-do.call("rbind", sim.result[3,])
	repl<-rep(1:length(sim.result[1,]), times= unlist(lapply(sim.result[3,], length))/length(sim.result[3,][[1]][1,]))
	temp<-cbind(repl, temp)
	summ<-tapply(temp[,"popsize"], list(temp[,"xloc"], temp[,"repl"]), mean, na.rm=T)
	summ.ps<-apply(summ, 1, mean)
	summ.ps.sd<-apply(summ, 1, sd)
	summ<-tapply(temp[,"mean.disp"], list(temp[,"xloc"], temp[,"repl"]), mean, na.rm=T)
	summ.tv<-apply(summ, 1, mean)
	summ.tv.sd<-apply(summ, 1, sd)
	summ<-tapply(temp[,"mean.h"], list(temp[,"xloc"], temp[,"repl"]), mean, na.rm=T)
	summ.h<-apply(summ, 1, mean)
	summ.h.sd<-apply(summ, 1, sd)
	summ<-tapply(temp[,"malad"], list(temp[,"xloc"], temp[,"repl"]), mean, na.rm=T)
	summ.mal<-apply(summ, 1, mean)
	summ.mal.sd<-apply(summ, 1, sd)
	pars<-sim.result[1,1][[1]]
	names(pars)<-c("lambda", "steps", "dcost", "dval")
	out<-t(matrix(pars, ncol=length(summ.tv), nrow=length(pars)))
	out<-cbind(out, 1:length(summ.ps), summ.ps, summ.ps.sd, summ.tv, summ.tv.sd, summ.h, summ.h.sd, summ.mal, summ.mal.sd)
	colnames(out)<-c(names(pars), "xloc", "ps.mean", "ps.sd", "tv.mean", "tv.sd", "h.mean", "h.sd", "mal.mean", "mal.sd")
	concat<-rbind(concat, out)
	rm(sim.result)	
}

save(concat, file="acSpace_adap.RData")


